var vm = new Vue({
    el: "#app",
    data: {
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        content: "",
        // allow: false,
        error_content: false,
        // allow_message:false
    },
    mounted: function () {
        // 一加载就需要做的，直接是代码
    },
    methods: {
        check_content: function () {

            var comment_len = this.content.length;
            if (comment_len < 0 || comment_len > 140) {
                this.error_content = false
            } else {
                this.error_content = true
            }
        },
        // check_allow: function () {
        //     if (!this.allow) {
        //         this.allow_message = false
        //         this.username = "匿名用户"
        //     } else {
        //         this.allow_message = true
        //         this.username = sessionStorage.username || localStorage.username
        //     }
        // },

        on_submit: function () {
            // this.check_allow();
            this.check_content();
            // if (this.allow_message == true && this.error_content == false) {
            if (this.error_content == false) {
                axios.post(HOST + "/spits/release/", {
                    content: this.content,
                    username:this.username,
                    user_id:this.user_id
                }, {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json'
                }).then(response => {
                    // window.location.href = "http://127.0.0.1:8080/spit-detail.html"
                    var return_url = get_query_string('next');
                    if (!return_url) {
                        return_url = '/spit-detail.html';
                    }
                    location.href = return_url;
                })
            }
        },
        logout: function () {
            sessionStorage.clear();
            localStorage.clear();
            location.href = '/spit-index.html';
        },

    }

})